//路由配置
(function() {
	var Service = APM.Service = createApp("service");

	function heightScroll(){
		setTimeout(function() {
			$(".application-main").css("overflow-y", "scroll");
			$(".application-main").height($(window).height() - 49);
			$(window).on("resize." + this.elementId, function() {
				$(".application-main").height($(window).height() - 49);
			});
		}, 10)
	}

	Service.routerMap(function() {
		//服务节点
		this.route("server", function() {
			this.route("follow");
			this.route("allserver");
			this.route("toposerver");
			this.route("nodeserver", { path: "/:node_id" }, function() {
				//服务概况
				this.route("overview");
				//错误码
				this.route("errorcode");
				//最新告警
				this.route("alarms");
				//动态菜单
				this.route("dynamic", { path: "/:menu_id" });
				//交易类型
				this.route("type");
				//交易渠道
				this.route("channel");
				//交易跟踪
				this.route("track");
			});
		});
	});

	APM.ServiceRoute = Service.Route.extend({
		templates: [{
			name: "main/frame",
			url: "/apps/main/tpls/main_frame.hbs"
		}],

		dependences: [
			"/apps/main/ctrls/frame_ctrl",
			"/apps/main/views/frame_view",
		],

		renderTemplate: function() {
			this.render("main/frame", {
				controller: "mainFrame",
				view: "mainFrame"
			});
		}
	});

	APM.ServiceIndexRoute = Service.Route.extend({
		redirect: function() {
			this.transitionTo("service.server");
		}
	});

	/*---------路由定义----------- */
	//服务节点
	APM.ServiceServerRoute = Service.Route.extend({
		dependences: [
			"./ctrls/server_ctrl",
			"./views/server_view",
			"./ctrls/allserver_ctrl"
		],
		model: function() {
			return this.store.ajax("GET", "nodes/serviceFocusCount")
				.then(function(response) {
					var serviceFocusCount = Ember.A(response.serviceFocusCount);
					return serviceFocusCount;
				});
		},
		setupController: function(ctrl, model, transition) {
			ctrl.set("serviceFocus", model.serviceFocus);
			ctrl.set("serviceAll", model.serviceAll);
		}
	});

	//服务节点--关注服务
	APM.ServiceServerFollowRoute = Service.Route.extend({
		dependences: [
			"./ctrls/follow_ctrl",
			"./views/follow_view"
		],
		model: function() {
			return this.store.ajax("GET", "alarms/serviceCount")
				.then(function(response) {
					var serRankNums = Ember.A(response.serRankNums);
					return serRankNums;
				});
		},
		setupController: function(ctrl, model, transition) {
			$('.route-service-server-follow').on('change', '.set-file input', function() {
				$('.route-service-server-follow').find('.file-name').text($(this).val())
			})
			$('#serviceDetialView').find('.breadcrumb').show();
			ctrl.set("serRankNums", model);
			ctrl.keyUpEnent();
			ctrl.initTime();
			ctrl.getserviceType();
			ctrl.set("serviceValue", "");
			ctrl.followResponse();
			overviewOrCenterSwitch = "system";
			heightScroll();
		},
		deactivate: function() {
			this.controller.set("model", null);
			this.controller.set("pageIndex", 0);
			this.controller.clearserverity();
		}
	});

	//服务节点--全部服务
	APM.ServiceServerAllserverRoute = Service.Route.extend({
		dependences: [
			"./ctrls/allserver_ctrl",
			"./views/allserver_view"
		],
		model: function() {
			return this.store.ajax("GET", "nodes/serviceFocusCount")
				.then(function(response) {
					var serviceFocusCount = Ember.A(response.serviceFocusCount);
					return serviceFocusCount;
				});
		},
		setupController: function(ctrl, model, transition) {
			$('#serviceDetialView').find('.breadcrumb').show();
			ctrl.set("serviceFocus", model.serviceFocus);
			ctrl.set("serviceAll", model.serviceAll);
			ctrl.set("serviceValue", "");
			ctrl.keyUpEnent();
			ctrl.allServerResponse();
			ctrl.getserviceType();
			heightScroll();
		},
		deactivate: function() {
			this.controller.set("model", null);
			this.controller.set("pageIndex", 0);
		}
	});

	//服务节点--服务拓扑图
	APM.ServiceServerToposerverRoute = Service.Route.extend({
		dependences: [
			"./ctrls/toposerver_ctrl.js?555555555",
			"./views/toposerver_view"
		],
		model: function() {
			return this.store.ajax("GET", "topology/services")
				.then(function(response) {
					return response;
				});
		},
		setupController: function(ctrl, model, transition) {
			$('#serviceDetialView').find('.breadcrumb').show();
			ctrl.set('model',model);
			ctrl.keyUpEnent();
			ctrl.topoServerRender();
		}
	});

	//服务节点--详情节点
	APM.ServiceServerNodeserverRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_ctrl",
			"./views/nodeserver_view"
		],
		model: function(params) {
			return this.store.ajax("GET", "nodes/disService", {
					"serviceId": params.node_id
				})
				.then(function(response) {
					var nodes = Ember.A(response.node);
					return nodes;
				});
		},
		setupController: function(ctrl, model, transition) {
			ctrl.set("model", model);
			$('#serviceDetialView').find('.breadcrumb').hide();
			heightScroll();
		}
	});

	APM.ServiceServerNodeserverIndexRoute = Service.Route.extend({
		redirect: function() {
			this.transitionTo("service.server.nodeserver.overview");
		}
	});


	//节点详情-服务概况
	APM.ServiceServerNodeserverOverviewRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_overview_ctrl",
			"./views/nodeserver_overview_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.startRealTimeMode();
			ctrl.setup(model);
		},
		deactivate: function() {
			this.controller.stopRealTimeMode();
		}
	});

	//节点详情-服务告警
	APM.ServiceServerNodeserverAlarmsRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_alarms_ctrl",
			"./views/nodeserver_alarms_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.keyUpEnent();
			ctrl.initTime();
			ctrl.requireAlerts();
			ctrl.set("model", model);
		},

	});

	//节点详情-服务错误
	APM.ServiceServerNodeserverErrorcodeRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_errorcode_ctrl",
			"./views/nodeserver_errorcode_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.keyUpEnent();
			ctrl.initTime();
			ctrl.set("model", model);
			ctrl.requireData();
			console.log(model);
			
		},
		deactivate: function() {
			// this.controller.set("model", null);
			this.controller.set("pageIndex", 0);
		}
	})

	//节点详情-交易类型
	APM.ServiceServerNodeserverTypeRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_type_ctrl",
			"./views/nodeserver_type_view"
		]
	});

	//节点详情-交易渠道
	APM.ServiceServerNodeserverChannelRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_channel_ctrl",
			"./views/nodeserver_channel_view"
		]
	});

	//节点详情-交易跟踪
	APM.ServiceServerNodeserverTrackRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_track_ctrl",
			"./views/nodeserver_track_view"
		]
	});

	//动态菜单
	APM.ServiceServerNodeserverDynamicRoute = Service.Route.extend({
		dependences: [
			"./ctrls/nodeserver_dynamic_ctrl",
			"./views/nodeserver_dynamic_view"
		],
		model: function(params, transition) {

			return this.store.ajax("GET", "node/menus", {
					nodeId: transition.resolvedModels["service.server.nodeserver"].id
				})
				.then(function(response) {
					var menu = Ember.A(response.result).findBy("id", parseInt(params.menu_id));
					return menu;
					// if(menu){

					// }else{
					// 	transition.abort();
					// 	this.transitionTo("service.server.nodeserver.overview");
					// }
				});
		},
		setupController: function(ctrl, model, transition) {
			ctrl.keyUpEnent();
			ctrl.initTime();
			ctrl.requireDataList1();
			ctrl.set("model", model);
		},
	});

})();